﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ECI.CommonLibrary;
using System.Data;
using System.Web;
using ECI.Entities;
using ECI.Controls;
using ECI.CoreLibrary;
using ECI.WebLib.Data;
using System.Web.UI;
using ECI.BaseLanguage;

namespace ECI.DynamicData.ElementBuilder
{
    public abstract class IElement
    {
        protected EciField Field = null;
        protected ElementParameter param = null;
        protected DynamicEditMode EditMode;
        protected TableCell MyTableCell = new TableCell();

        public void Init(ElementParameter param)
        {
            this.Field = param.Field;
            this.param = param;
            this.EditMode = param.editMode;
        }

        protected TableCell GetControlCell(Table tb, string txtId)
        {
            bool f = false;
            TableCell cellResult = null;
            foreach (TableRow tr in tb.Rows)
            {
                if (f)
                    break;
                foreach (TableCell cell in tr.Cells)
                {
                    foreach (Control ctrl in cell.Controls)
                    {
                        if (ctrl.ID == txtId)
                        {
                            cellResult = cell;
                            f = true;
                            break;
                        }
                    }
                }
            }
            return cellResult;
        }

        protected void CreateContainer()
        {
            MyTableCell = new TableCell();//创建文本框的容器Cell
            MyTableCell.VerticalAlign = VerticalAlign.Top;
            MyTableCell.Attributes.Add("style", "white-space:nowrap");
            MyTableCell.Style["width"] = param.CellWidth;

            //如果是编辑画面考虑COLSPAN参数
            int colSpan = 0;
            if (param.type == "Edit")
            {
                if (param.Field.Colspan.HasValue)
                {
                    if (param.Field.Colspan.Value > 1)
                    {
                        MyTableCell.ColumnSpan = Convert.ToInt32(param.Field.Colspan);
                        colSpan = MyTableCell.ColumnSpan;
                    }
                }
            }


            string myVerticalAlign = param.Field.VerticalAlign.ToMyString();

            if (myVerticalAlign == "1")
            {
                MyTableCell.VerticalAlign = VerticalAlign.Top;
            }

            if (myVerticalAlign == "2")
            {
                MyTableCell.VerticalAlign = VerticalAlign.Middle;
            }

            if (myVerticalAlign == "3")
            {
                MyTableCell.VerticalAlign = VerticalAlign.Bottom;
            }
        }

        public abstract void Create(ElementParameter param);
        /// <summary>
        /// 控件锁定控制
        /// </summary>
        /// <param name="param"></param>
        public abstract void Locked();
        /// <summary>
        /// 默认值设置
        /// </summary>
        /// <param name="param"></param>
        public abstract void Default();

        /// <summary>
        /// 系统字段锁定控制
        /// </summary>
        /// <param name="param"></param>
        public abstract void SystemField();

        /// <summary>
        /// 级联控制
        /// </summary>
        /// <param name="param"></param>
        public abstract void Cascade();

        /// <summary>
        /// 数据保持功能
        /// </summary>
        public abstract void DataKeep();
        /// <summary>
        /// 查询默认值设定
        /// </summary>
        public abstract void QueryDefault();

        /// <summary>
        /// 客户端事件注册
        /// </summary>
        public abstract void ClientEventRegister();

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="param"></param>
        public abstract string GetValue();

        public abstract string GetRealValue();

        public string GetSystemDataValue()
        {
            string result = "";
            switch (Field.SystemData.ToLower())
            {
                case "{sysdate}": result = DateTime.Now.ToFullString(); break;
                case "{userid}": result = UserInfo.UserId; break;
                case "{username}": result = UserInfo.UserName; break;
                case "{roleid}": result = UserInfo.RoleId; break;
                case "{operside}": result = UserInfo.OperSide; break;
                case "{opersidename}": result = UserInfo.OperSideName; break;
                case "{companycode}": result = UserInfo.CompanyCode; break;
                case "{companyname}": result = UserInfo.CompanyName; break;
                default: throw new Exception(Lang.A00082.Read() + ":" + Field.SystemData);//配置的系统数据不正确
            }
            return result;
        }

        public string GetSystemDataValueForChoose()
        {
            string result = "";
            switch (Field.SystemData.ToLower())
            {
                case "{userid}": result = UserInfo.UserId + "|" + UserInfo.UserName; break;
                case "{roleid}": result = UserInfo.RoleId + "|" + UserInfo.RoleName; break;
                case "{operside}": result = UserInfo.OperSide + "|" + UserInfo.OperSideName; break;
                case "{companycode}": result = UserInfo.CompanyCode + "|" + UserInfo.CompanyName; break;
                default: throw new Exception(Lang.A00082.Read() + ":" + Field.SystemData);//配置的系统数据不正确
            }

            if (ECI.CoreLibrary.PlatformInfo.DevelopMode)
            {
                LOG.Write(result, "SystemData");
            }
            return result;
        }

        protected string GetDataValueForEditPage()
        {
         
            string value;
            string fieldDataSource = Field.DataSource.Trim();
            if (fieldDataSource.Length == 0)
            {
                value = param.DataRow[param.Field.Name].ToString();
            }
            else
            {
                //if (!param.DataSource.Tables.Contains(fieldDataSource)) throw new Exception("不存在数据项" + Field.MainAlias + "配置的数据源" + fieldDataSource);
                if (!param.DataSource.Tables.Contains(fieldDataSource)) throw new Exception(Lang.A00083.Read() + Field.MainAlias + Lang.A00084.Read() + fieldDataSource);
                DataTable data = param.DataSource.Tables[fieldDataSource];
                //if (!data.Columns.Contains(Field.Name)) throw new Exception("数据源" + fieldDataSource + "中不存在数据项" + Field.Name);
                if (!data.Columns.Contains(Field.Name)) throw new Exception(Lang.A00085.Read() + fieldDataSource + Lang.A00086.Read() + Field.Name);
                if (data.Rows.Count > 0)
                {
                    value = data.Rows[0][Field.Name].ToString();
                }
                else
                {
                    value = "";
                }
            }

            return value;
        }

        protected string GetDefaultDataForControl()
        {
            string value = Field.DefaultValue;
            string result = value;
            result = ECI.CommonLibrary.SQLParamConvert.Convert(result);

            return result;
        }

        protected string GetQueryDefaultDataForControl()
        {
            string value = Field.DefaultValueQuery;
            string result = value;
            result = ECI.CommonLibrary.SQLParamConvert.Convert(result);

            return result;
        }

        public void CreateEditTip()
        {
            if (this.Field.EditTip.ToMyString().Trim() != "")
            {
                Label lb = new Label();
                lb.Text = this.Field.EditTip.ToMyString().Trim();
                lb.Style.Add("color", "red");
                this.MyTableCell.Controls.Add(lb);
            }
        }
    }
}
